package com.example.user_module.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.room.Room;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.base_application_sdk.BaseApplication;
import com.example.common.mvvm.view.BaseActivity;
import com.example.user_module.databinding.ActivityLoginBinding;
import com.example.user_module.room.AppDatabase;
import com.tencent.connect.UserInfo;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

@Route(path = "/user/loginactivity")
public class LoginActivity extends BaseActivity<ActivityLoginBinding,LoginViewModel> implements IUiListener {

    private Tencent tencent;
    private String openid= "";

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        ActivityLoginBinding activityLoginBinding=ActivityLoginBinding.inflate(inflater);
        return activityLoginBinding;
    }

    @Override
    public void initView() {
        //初始化腾讯SDK
        tencent=Tencent.createInstance("101794421", getApplicationContext());
        tencent.login(LoginActivity.this,"all",LoginActivity.this);
    }

    @Override
    public void initData() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode== Constants.REQUEST_LOGIN){
            Tencent.onActivityResultData(requestCode, resultCode, data, this);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    //授权完成
    @Override
    public void onComplete(Object o) {
        JSONObject jsonObject= (JSONObject) o;
        Iterator<String> keys = jsonObject.keys();
        while (keys.hasNext()){
            String key = keys.next();
            Log.i("aaa",key);
            //用户登录接口返回值
            if (key.equals("openid")){
                try {
                    openid = jsonObject.getString("openid");
                    String access_token = jsonObject.getString("access_token");
                    String expires_in = jsonObject.getString("expires_in");
                    tencent.setAccessToken(access_token,expires_in);
                    tencent.setOpenId(openid);
                    //配置获取qq用户信息请求
                    UserInfo info=new UserInfo(this,tencent.getQQToken());
                    info.getUserInfo(this);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return;
            }
        }
        try {
            String figureurl = jsonObject.getString("figureurl");
            String nickname = jsonObject.getString("nickname");
            String constellation = jsonObject.getString("constellation");

            Log.i("ABC","figureurl:"+figureurl);
            Log.i("ABC","nickname:"+nickname);
            Log.i("ABC","constellation:"+constellation);

            vm.requestUser(figureurl,"0",System.currentTimeMillis(),0,0,nickname,openid,0);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //出错
    @Override
    public void onError(UiError uiError) {
        Log.e("onError: ",uiError.errorMessage);
    }

    //取消
    @Override
    public void onCancel() {
        Log.e( "onCancel: ","取消登录");
    }
}
